﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Threading;
using System.Reflection; // 引用这个才能使用Missing字段
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Net;
using System.IO;
using CCWin;
using Newtonsoft.Json;
using LitJson;
using Newtonsoft.Json.Linq;

namespace weiqijiance.声级计
{
    public class nobass
    {
        public float fshengji = 0;
        public bool bopen = false;
        public void init()
        {
            iniclass objini = new iniclass();
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\安综检仪表配置.ini";
            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);
            string sSetCom = objini.ReadValue("声级", "声级计串口设置").Trim();
            if (sSetCom != "")
            {
                openCOM(sSetCom, comNO);
                comNO.ReadTimeout = 500;
                //comNO.ReceivedBytesThreshold = 10;//达到50字节以后才开始触发串口
                comNO.DataReceived += comNO_DataReceived;
            }
        }
        private SerialPort comNO = new SerialPort();//声级串口

        public bool openCOM(string sSet1, SerialPort commex)//打开串口
        {

            string[] szbzlCOM = new string[5];
            if (sSet1.Length > 0)
                szbzlCOM = sSet1.Split(':');
            //打开串口
            commex.PortName = szbzlCOM[0];
            commex.BaudRate = Convert.ToInt32(szbzlCOM[1]);

            commex.NewLine = "/r/n";
            commex.RtsEnable = true;//根据实际情况吧。 
            commex.ReadTimeout = 300;//读取数据延时，避免读取不全数据

            if (szbzlCOM[3] == "0")
                commex.Parity = Parity.None;
            else
                commex.Parity = Parity.Even;
            // ////添加事件注册  
            //  comm.DataReceived += comm_Data;
            try
            {
                commex.Open();
                bopen = true;
            }
            catch (Exception)
            {
                //lzj20200706 MessageBox.Show(string.Format("{0}\n请检查电脑的串口故障", ex.Message));
                return false;
            }
            return true;
        }
        ////  声级
        void comNO_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (comNO.IsOpen)
            {
                bopen = true;
                Thread.Sleep(30);
                int n = comNO.BytesToRead;//
                byte[] buf1 = new byte[n];//声明一个临时数组存储当前来的串口数据  
                byte[] buf = new byte[n];
                comNO.Read(buf1, 0, n);
                //41 57 41 41 
                //2C 20 34 39 2E 31 
                //64 42 41 2C 45 03
                if (n >= 16)
                {
                    buf = null;
                    buf = new byte[n];
                    buf1.CopyTo(buf, 0);
                    for (int i = 0; i < n-10; i++)
                    {
                        if (buf[0 + i] == 0x41 && buf[1 + i] == 0x57 && buf[2 + i] == 0x41 && buf[3 + i] == 0x41)
                        {
                            //float.TryParse(string.Format("{0}", (buf[5] * 256 + buf[3]) * 0.1), out fshengji);
                            if (buf[5 + i] == 0x20)
                            {
                                fshengji = (buf[6 + i] - 0x30) * 10f + (buf[7+i] - 0x30) + (buf[9 + i] - 0x30) * 0.1f;
                                break;
                            }
                            else
                            {
                                fshengji = (buf[5 + i] - 0x30) * 100f + (buf[6 + i] - 0x30) * 10f + (buf[7 + i] - 0x30) + (buf[9 + i] - 0x30) * 0.1f;
                                break;
                            }
                            //float.TryParse(string.Format("{0}", (buf[2] * 256 + buf[3]) * 0.1), out fshengji);
                        }
                    }
                }
            }
        }
    }
}
